<?php
namespace App\Http\Controllers;
use App\ErrorDesc;
use Laravel\Lumen\Routing\Controller as BaseController;
use App\ErrorCode;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class ArticleController extends Controller
{
    public function __construct(Request $request)
    {
        $this->setParam($request);
    }
    //添加文章
    public function createArticle(){
        $bool = DB::table("article")->insert([
            'articleTitle' => $this->getParam('articleTitle'),
            'articleSummary' => $this->getParam('articleSummary'),
            'articleContent' => $this->getParam('articleContent'),
            'articleType' => $this->getParam('articleType'),
            'createTime' => time()
        ]);
        if($bool){
            return $this->output('添加成功');
        }
    }
    //获取文章
    public function getArticleList(){
        $articleTitle=$this->getParam('articleTitle');
        $ispublish=$this->getParam('ispublish');
        $start =$this->getParam('start');
        $num = $this->getParam('num');
        $keyWord = $this->getParam('keyWord');
        $strWhere = ' 1=1';
        $param = [];
        if (!empty($articleTitle)) {
            $strWhere .= ' AND articleTitle LIKE ? ';
            $param[] = '%' . $articleTitle . '%';

        }
        if (!empty($keyWord)) {
            $strWhere .= ' AND articleTitle LIKE ? ';
            $param[] = '%' . $keyWord . '%';

        }
        if (!empty($ispublish)) {
            $strWhere .= ' AND ispublish = ? ';
            $param[] = $ispublish;

        }
        $count = DB::table('article')
            ->whereRaw($strWhere, $param)
            ->count();
        if ((int)$num !== -1) {
            $list = DB::table('article')
                ->whereRaw($strWhere, $param)
                ->orderBy('createTime', 'desc')
                ->take($num)
                ->skip($start)
                ->get();
        } else {
            $list = DB::table('article')
                ->whereRaw($strWhere, $param)
                ->orderBy('createTime', 'desc')
                ->get();
        }
        $res['count'] = $count;
        $res['items'] = $list;
        return $this->output($res);

    }
    //发布文章
    public function ispublish(){
        $articleId=$this->getParam('articleId');
        $bool=DB::table('article')
            ->whewe('articleId','=',$articleId)
            ->update([
                'ispublish'=>2
            ]);
        if($bool){
            return $this->output('发布成功');
        }
    }
    //修改文章
    public function updateArticle(){
        $ArticleId=$this->getParam('ArticleId');

        if(empty($ArticleId)){
            return $this->output(false,ErrorCode::PARAM_ERROR);
        }
        $num=DB::table('article')
            ->where('articleId',$ArticleId)
            ->update([
                'articleTitle'=>$this->getParam('articleTitle'),
                'articleSummary'=>$this->getParam('articleSummary'),
                'articleContent'=>$this->getParam('articleContent'),
                'fileId'=>$this->getParam('fileId'),
                'updateTime'=>time()
            ]);
        if(!$num){
            return $this->output(false,ErrorCode::OPERATION_ERROR);
        }
        return $this->output('修改成功');
    }
    //删除文章
    public function deleteArticle(){
      $ArticleIds=$this->getParam('ArticleIds');
        if(empty($ArticleIds)){
            return $this->output(false,ErrorCode::PARAM_ERROR);
        }
        foreach ($ArticleIds as $ArticleId){
            $num = DB::table('article')
                ->where('articleId',$ArticleId)
                ->delete();
            if(!$num){
                return $this->output(false,ErrorCode::OPERATION_ERROR);
            }
        }
        return $this->output('删除成功');
    }
    //获取单篇文章
    public function getarticle(){
        $id=$this->getParam('articleId');
        $detil=DB::table('article')
            ->where('articleId','=',$id)
            ->get();
        return $this->output($detil);
    }
    //获取置顶文章
    public  function getzdList(){
        $list=DB::table('article')
            ->where('isup','=',1)
            ->get();
        return $this->output($list);
    }
    //是否置顶
    public function isup(){
        $articleId=$this->getParam('articleId');
        $isup=$this->getParam('isup');
        if($isup==1) {
            $bool = DB::table('article')
                ->where('isup', '=', $isup)
                ->count();
            if ($bool >= 4) {
                return $this->output(false,ErrorCode::OPERATION_ERROR);
            }
        }
        $bool=DB::table('article')
            ->where('articleId','=',$articleId)
            ->update([
                'isup'=>$isup
            ]);
        if($bool){
            return $this->output('置顶成功');
        }
    }
}